package com.taotao.portal.controller;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.portal.pojo.SearchResult;
import com.taotao.portal.service.SearchService;

@Controller
public class SearchController {
	@Autowired
	private SearchService searchService;

	/**
	 * 搜索请求逻辑视图解析
	 * 
	 * @param keywords
	 * @param page
	 * @param rows
	 * @param model
	 * @return
	 * @author: JSJ
	 */
	@RequestMapping("/search")
	public String search(
			@RequestParam(value = "q", defaultValue = "") String keywords,
			@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "60") Integer rows, Model model) {
		try {
			// get请求中文乱码处理
			keywords = new String(keywords.getBytes("ISO-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			keywords = "";
			e.printStackTrace();
		}
		SearchResult result = searchService.serach(keywords, page, rows);
		// 设置返回的请求数据
		model.addAttribute("query", keywords);
		model.addAttribute("totalPages", result.getPageCount());
		model.addAttribute("itemList", result.getItemList());
		model.addAttribute("page", result.getCurPage());
		// 返回逻辑视图
		return "search";
	}
}
